package com.guotd.ticketSystem.controller;

import com.guotd.ticketSystem.mapper.AirlineMapper;
import com.guotd.ticketSystem.mapper.FlightMapper;
import com.guotd.ticketSystem.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addAirline")
public class AddAirlineServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            String id = request.getParameter("id");
            String fromAirport = request.getParameter("fromAirport");
            String toAirport = request.getParameter("toAirport");
            try {
                SqlSession session = SessionUtil.getSqlSession();
                AirlineMapper airlineMapper = session.getMapper(AirlineMapper.class);
                int row = airlineMapper.addAirline(id,fromAirport,toAirport);
                session.commit();
                session.close();
                if (row > 0) {
                    request.getSession().setAttribute("airline_msg", "添加成功！");
                    request.getRequestDispatcher("getAirlines").forward(request, response);
                }
            } catch (Exception e) {
                request.setAttribute("airline_msg", "添加失败，该航线已存在或未找到对应的机场！");
                request.getRequestDispatcher("airline_add.jsp").forward(request, response);
            }
        }
}
